class FiveChessGameScene extends egret.Sprite {
	private _chessBoardBg: egret.Shape;
	private _startBtn: eui.Button;
	private _msgLabel: egret.TextField;

	/** 棋子持可以用来悔棋 */
	private _peiceArray: Array<any> = [];

	private _wins: Array<any>;
	private _count: number;
	/** 最后一手的棋子颜色 */
	private _lastRole: number = 0;
	/**是否结束 */
	private _isEnd: boolean = true;
	/** 是否先手,默认人优先 */
	private _firstRole: number = chess.ChessConst.role.hum;

	private chessBoard: chess.ChessBoard;
	/** 负极值搜索算法 */
	private negaMaxSearch: ai.NegaMaxSearch;

	public constructor() {
		super();
		this.touchEnabled = true;
		this._wins = ai.WinUtils.getInstance().getWinArray().wins;
		this._count = ai.WinUtils.getInstance().getWinArray().count;
		this.chessBoard = new chess.ChessBoard();
		this.negaMaxSearch = new ai.NegaMaxSearch();
		this.negaMaxSearch.board = this.chessBoard;
		this.initScene();
		this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAdd, this)
		this.addEventListener(egret.Event.REMOVED_FROM_STAGE, this.onRemove, this)

	}

	/** 初始化游戏场景 */
	public initScene() {
		let self = this;
		self._chessBoardBg = new egret.Shape();
		self._chessBoardBg.graphics.beginFill(0xC4C4C4, 0.1);
		self._chessBoardBg.graphics.drawRect(-20, -20, 600, 600);
		self._chessBoardBg.graphics.endFill();
		self.addChild(self._chessBoardBg);
		self._chessBoardBg.graphics.beginFill(0xff0000, 1);
		self._chessBoardBg.graphics.lineStyle(2, 0x00ff00, 0.8);
		for (let i = 0; i < 15; i++) {
			self._chessBoardBg.graphics.moveTo(0, i * 40);
			self._chessBoardBg.graphics.lineTo(560, i * 40);
			self._chessBoardBg.graphics.moveTo(i * 40, 0);
			self._chessBoardBg.graphics.lineTo(i * 40, 560);
		}
		self._chessBoardBg.graphics.endFill();
		self._chessBoardBg.touchEnabled = true;

		self._startBtn = new eui.Button();
		self._startBtn.label = "开始游戏";
		self._startBtn.y = self._chessBoardBg.height + 50;
		self._startBtn.x = (self.width - self._startBtn.width) * 0.5 - 50;
		self.addChild(self._startBtn);

		self._msgLabel = new egret.TextField();
		self._msgLabel.text = "游戏信息";
		self._msgLabel.y = -80;
		self._msgLabel.x = (self.width - self._msgLabel.width) * 0.5;
		self.addChild(self._msgLabel);
	}

	public beginGame(e: egret.TouchEvent) {
		let self = this;
		self.initChessData();
		self.chessBoard.init(15);
		self._isEnd = false;
		if (self._firstRole == chess.ChessConst.role.hum) {

		}
		e.stopImmediatePropagation();
	}

	private createPiece(e: egret.TouchEvent) {
		let self = this;
		if (self._isEnd) {
			this._msgLabel.text = "请点击开始游戏";
			return;
		} else if (self._lastRole == chess.ChessConst.role.hum) {
			this._msgLabel.text = "等待电脑落子";
			return;
		}
		let x = e.localX;
		let y = e.localY;
		if (-20 > x || x > 620 || -20 > y || y > 620) return;
		//判断该点离在方格的4个区域中的哪个
		let positionX = Math.round(x / 40);
		let positionY = Math.round(y / 40);
		positionX = positionX <= 0 ? 0 : positionX;
		positionY = positionY <= 0 ? 0 : positionY;
		if (self.chessBoard.board[positionX][positionY] != 0) {
			self._msgLabel.text = "请选择空白区域落子";
			return;
		}
		self.putPeice(positionX, positionY);
		egret.setTimeout(() => { self.aiPutPeice(); }, this, 100);

	}

	/** 落子 */
	public putPeice(positionX, positionY) {
		let self = this;
		if (self._isEnd) return;
		//执黑先行
		if (self._lastRole == 0) self._lastRole = self._firstRole;
		else this._lastRole = chess.ChessConst.role.reverse(self._lastRole);
		let peiceType = (self._peiceArray.length + 1) % 2;
		let peice = new ChessPeice(positionX, positionY, peiceType, 20);
		self._peiceArray.push(peice);
		this.chessBoard.put([positionX, positionY], self._lastRole);
		self.addChild(peice);
		self._msgLabel.text = chess.ChessConst.roleName[peiceType] + `落子[${positionX + 1},${positionY + 1}]`;
		if (ai.WinUtils.getInstance().isFive(self.chessBoard.board, [positionX, positionY], this._lastRole)) {
			self._msgLabel.text = chess.ChessConst.roleName[peiceType] + "获胜";
			self._isEnd = true;
		}
	}

	/** ai落子 */
	public  aiPutPeice() {
		let p, self = this;
		if (self._isEnd) return;
		if (self.chessBoard.allSteps.length > 1) p = ai.Opening.getInstance().match(self.chessBoard);
		p = p || this.negaMaxSearch.deepAll(undefined, chess.ChessConst.config.searchDeep);
		self.chessBoard.put(p, chess.ChessConst.role.com);
		self.putPeice(p[0], p[1]);
		// 电脑自己对自己
		// egret.setTimeout(() => { self.aiPutPeice(); }, this, 100);
	}

	private onAdd(e: egret.Event) {
		let self = this;
		self._startBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, self.beginGame, self)
		self.addEventListener(egret.TouchEvent.TOUCH_TAP, self.createPiece, self);
	}

	private onRemove(e: egret.Event) {
		let self = this;
		self._startBtn.removeEventListener(egret.TouchEvent.TOUCH_TAP, self.beginGame, self)
		self.removeEventListener(egret.TouchEvent.TOUCH_TAP, self.createPiece, self);
	}


	private initChessData() {
		let self = this;
		while (self._peiceArray.length) {
			let peice = self._peiceArray.shift();
			self.removeChild(peice);
		}
		self._lastRole = 0;
		self._msgLabel.text = "游戏信息";
	}
}

class ChessPeice extends egret.Shape {

	private _postionX: number;
	private _postionY: number;
	private _type: number; // 1黑 2 白
	private _radius: number; //棋子半径

	public static chessBoardSize = 40;

	public constructor(positionX, positionY, type, radius) {
		super();
		this.postionX = positionX;
		this.postionY = positionY;
		this._type = type;
		this._radius = radius;
		this.drawPeice();
	}

	public drawPeice() {
		this.graphics.clear();
		if (this._type == 1) this.graphics.beginGradientFill(egret.GradientType.RADIAL, [0x000000, 0xffffff], [1, 0], [0, 255]);
		else if (this._type == 0) this.graphics.beginGradientFill(egret.GradientType.RADIAL, [0xffffff, 0x000000], [1, 0], [0, 255]);
		else return;
		this.graphics.drawCircle(0, 0, this._radius);
		this.graphics.endFill();
	}

	public get postionX(): number {
		return this._postionX;
	}

	public set postionX(value: number) {
		this.x = ChessPeice.chessBoardSize * value;
		this._postionX = value;
	}
	public get postionY(): number {
		return this._postionY;
	}

	public set postionY(value: number) {
		this.y = ChessPeice.chessBoardSize * value;
		this._postionY = value;
	}

	public get type() {
		return this._type;
	}

	public set type(value: number) {
		this._type = value;
		this.drawPeice();
	}
}